package com.elong.businesstravel.modules.home.hotel.search;

import static android.widget.Toast.LENGTH_SHORT;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.timessquare.CalendarCellView;
import com.android.support.jhf.timessquare.CalendarPickerView;
import com.android.support.jhf.timessquare.MonthCellDescriptor;
import com.android.support.jhf.timessquare.CalendarPickerView.OnDateSelectedListener;
import com.android.support.jhf.timessquare.CalendarPickerView.OnInvalidDateSelectedListener;
import com.android.support.jhf.timessquare.CalendarPickerView.SelectionMode;
import com.android.support.jhf.utils.DateUtils;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.utils.SaveUserOperate;
import com.elong.businesstravel.base.view.TitleNavBarView;

/**
 * 酒店预订日期
 * 
 * @author lenovo
 * 
 */
public class HotelDestineDateActivity extends BaseActivity {

	private String mSearchDestineDateString;
	private String mSearchLiveDaysString;

	private CalendarPickerView calendar;

	private Calendar minCalendar;
	private Calendar maxYear;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hotel_destione_date);

		init();
	}

	@Override
	protected void init() {
		
		checkDate();

		minCalendar = Calendar.getInstance();
		minCalendar.setTimeInMillis(DateUtils.getDateMillis(
				mApplication.mHotelDestineDateStrings[0], "yyyy-MM-dd (E)"));

		maxYear = Calendar.getInstance();
		maxYear.setTimeInMillis(DateUtils
				.getDateMillis(
						mApplication.mHotelDestineDateStrings[mApplication.mHotelDestineDateStrings.length - 1],
						"yyyy-MM-dd (E)"));

		calendar = (CalendarPickerView) findViewById(R.id.calendar_view);

		String valueString1 = "" + "\n入住";
		SpannableString spannableString1 = new SpannableString(valueString1);

		String valueString2 = "" + "\n离店";
		SpannableString spannableString2 = new SpannableString(valueString2);

		calendar.setFirstLastValue(spannableString1, spannableString2);

		ArrayList<Date> dates = new ArrayList<Date>();
		try {
			ArrayList<Date> tempDates = SaveUserOperate
					.getInOutHotelDates(mContext);
			dates.add(tempDates.get(0));
			dates.add(tempDates.get(tempDates.size() - 1));
		} catch (Exception e) {
		}

		if (0 == dates.size()) {
			// 第一次进入应用没有保存默认选中
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(System.currentTimeMillis());
			Date startDate = calendar.getTime();
			calendar.add(Calendar.DAY_OF_YEAR, 1);
			Date endDate = calendar.getTime();
			dates.add(startDate);
			dates.add(endDate);
		}
		calendar.init(minCalendar.getTime(), maxYear.getTime()) //
				.inMode(SelectionMode.RANGE) //
				.withSelectedDates(dates);

		setTitleNavBar();

		setListener();
	}
	
	@Override
	protected void refreshDateUI(String showDateUIString) {
		super.refreshDateUI(showDateUIString);
		init();
	}

	@Override
	protected void setListener() {
		calendar.setOnDateSelectedListener(new OnDateSelectedListener() {

			private MonthCellDescriptor lastMonthCellDescriptor = null;

			@Override
			public void onDateUnselected(Date date) {
			}

			@Override
			public void onDateSelected(Date date,
					MonthCellDescriptor monthCellDescriptor) {

				List<Date> dateList = calendar.getSelectedDates();

				if (dateList.size() == 1) {
					if (null != lastMonthCellDescriptor) {
						lastMonthCellDescriptor
								.setValue(lastMonthCellDescriptor
										.getTempValue());
					}
					String valueString = monthCellDescriptor.getTempValue()
							+ "\n入住";
					SpannableString spannableString = new SpannableString(
							valueString);
					spannableString.setSpan(new ForegroundColorSpan(mContext
							.getResources().getColor(android.R.color.white)),
							0, valueString.length(),
							Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					float moneySize = 12;
					spannableString.setSpan(new AbsoluteSizeSpan(
							(int) moneySize), valueString.length() - 2,
							valueString.length(),
							Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					monthCellDescriptor.setValue(spannableString);
					lastMonthCellDescriptor = monthCellDescriptor;
				} else if (dateList.size() > 1) {
					String valueString = monthCellDescriptor.getTempValue()
							+ "\n离店";
					SpannableString spannableString = new SpannableString(
							valueString);
					spannableString.setSpan(new ForegroundColorSpan(mContext
							.getResources().getColor(android.R.color.white)),
							0, valueString.length(),
							Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					float moneySize = 12;
					spannableString.setSpan(new AbsoluteSizeSpan(
							(int) moneySize), valueString.length() - 2,
							valueString.length(),
							Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					monthCellDescriptor.setValue(spannableString);
				}

				if (dateList.size() > 1) {
					if (dateList.size() <= 20) {
						try {
							// 保存入住离店时间
							SaveUserOperate.setInOutHotelDates(mContext,
									(ArrayList<Date>) dateList);
						} catch (Exception e) {
						}

						mSearchLiveDaysString = (dateList.size() - 1) + "晚";
						mSearchDestineDateString = DateUtils.dateFormat(
								dateList.get(0).getTime(), "yyyy-MM-dd (E)");
						Intent intent = new Intent();
						intent.putExtra("days", mSearchLiveDaysString);
						intent.putExtra("date", mSearchDestineDateString);
						setResult(RESULT_OK, intent);
						quit();
					} else {
						ArrayList<Date> dates = new ArrayList<Date>();
						calendar.init(minCalendar.getTime(), maxYear.getTime()) //
								.inMode(SelectionMode.RANGE) //
								.withSelectedDates(dates);
						HandlerToastUI.getHandlerToastUI(mContext, "最多只能住20天");
					}
				}

			}
		});
		calendar.setOnInvalidDateSelectedListener(new OnInvalidDateSelectedListener() {

			@Override
			public void onInvalidDateSelected(Date date) {
				// HandlerToastUI.getHandlerToastUI(mContext, "当前日期不能选择");
			}
		});
	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("选择入住离店日期");
		titleNavBarView.setTitleBackground(R.color.bg_title_dialog);
		titleNavBarView.setLeftButtonImageString(0, "取消", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				quit();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	private void quit() {
		finish();
		overridePendingTransition(R.anim.activity_alpha_fixed,
				R.anim.activity_top_bottom_exit_anim_translate);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			quit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 0: {
			// 入住天数返回
			switch (resultCode) {
			case RESULT_OK: {
				mSearchLiveDaysString = data.getStringExtra("days");
				Intent intent = new Intent();
				intent.putExtra("days", mSearchLiveDaysString);
				intent.putExtra("date", mSearchDestineDateString);
				setResult(RESULT_OK, intent);
				quit();
				break;
			}
			case RESULT_CANCELED: {
				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
	}

}
